/**************************************************************************//**
\file  battery.c

\brief This module is used for measurment battery voltage.

\author
    Atmel Corporation: http://www.atmel.com \n
    Support email: avr@atmel.com

  Copyright (c) 2008-2012, Atmel Corporation. All rights reserved.
  Licensed under Atmel's Limited License Agreement (BitCloudTM).

\internal
  History:
    29/06/07 E. Ivanov - Created
******************************************************************************/
#if APP_DISABLE_BSP != 1

/******************************************************************************
                   Includes section
******************************************************************************/
#include <battery.h>
#include <bspadc.h>
#include <bspTaskManager.h>
#include <types.h>

/******************************************************************************
                   Define(s) section
******************************************************************************/
/******************************************************************************
                   Types section
******************************************************************************/
// battery states
typedef enum
{
  IDLE,      // idle
  BUSY,      // opened and ready to be used
  DATA       // performs request
} batteryStates_t;

// battery descriptor
typedef struct
{
  uint16_t batteryData;
  BspBatteryCb_t batteryCallback; // callback
} BatteryControl_t;

/******************************************************************************
                   Global variables section
******************************************************************************/
// Monitors current state
static batteryStates_t batteryState = IDLE;
static BatteryControl_t batteryControl;

/******************************************************************************
                   Implementations section
******************************************************************************/
/**************************************************************************//**
\brief Opens the component to use.
\return
  BC_SUCCESS - the component is ready to been use. \n
  BC_FAIL - otherwise.
******************************************************************************/
result_t openBattery(void)
{
  if (IDLE == batteryState)
  {
		//openBspAdc();
		batteryState = BUSY;
	    return BC_SUCCESS;
  }
  return BC_FAIL;
}

/**************************************************************************//**
\brief  Closes component.
\return
  BC_SUCCESS - always.
******************************************************************************/
result_t closeBattery(void)
{
  if (IDLE == batteryState)
    return BC_FAIL;
  batteryState = IDLE;
  //closeBspAdc();
  return BC_SUCCESS;
}


/**************************************************************************//**
\brief  Starts ADC request on battery channel. XXXXXXXXXXXX
******************************************************************************/
void batteryDataCallback(uint16_t data)
{
	batteryState = BUSY;
	batteryControl.batteryCallback(data * 5ul);
}

/**************************************************************************//**
\brief  Starts ADC request on battery channel.
\param[in]
    data - sensor data.
\return
    BC_FAIL - battery component was not opened. \n
    BC_SUCCESS - other case.
******************************************************************************/
result_t readBatteryData(BspBatteryCb_t cb)
{

	if (BUSY != batteryState)
		return BC_FAIL;
	batteryState = DATA;
	batteryControl.batteryCallback = cb;

	readBspAdc(batteryDataCallback, BSP_ADC_CHANNEL0);
  
	return BC_SUCCESS;
}

#endif // APP_DISABLE_BSP != 1

// eof battery.c
